package de.teamgamma.cansat.groundstation.server;

import java.util.ArrayList;

import org.json.JSONObject;

import de.teamgamma.cansat.groundstation.transmitting.TransmitOptions;
import de.teamgamma.cansat.groundstation.usb.Receiver;

/**
 * Class that creates messages for the Java Socket Server
 * @author Alexander Ciupka
 *
 */

public class Messages {

	private ArrayList<String> messages = new ArrayList<String>();
	private JSONObject json;
	private String oldJson;
	private Receiver receiver;
	private TransmitOptions transmitOptions;

	public Messages() {
		buildInstances();
	}
	
	/**
	 * adds a new message to the messages ArrayList
	 * @param string New message which will add to the ArrayList
	 */
	
	public void input(String string) {
		messages.add(string);
	}

	/**
	 * gets The last message
	 * @return the last message from the ArrayList
	 */
	
	public String sendMessages() {
		if (messages.size() != 0) {
			String ausgabe = messages.get(messages.size() - 1);
			messages.remove(messages.size() - 1);
			return ausgabe;
		} else {
			return null;
		}
	}

	/**
	 * adds the received JSON to the messages ArrayList
	 */
	
	public void input() {
		if (!oldJson.equals(json.toString()) && transmitOptions.checkSendToSmartphone()) {
			json = receiver.getData();
			messages.add(json.toString());
			oldJson = json.toString();
		}
	}

	/**
	 * checks if there is any message in the ArrayList
 	 * @return boolean which shows if there is an message in the ArrayList
	 */
	
	public boolean next() {
		if (messages.size() != 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public void buildInstances() {
		receiver = Receiver.getInstance();
		transmitOptions = TransmitOptions.getInstance();
	}

}
